"""
请求工具：requests
解析工具：re
"""
import time

import requests
import re

res = requests.get("https://www.hdbz.net/")
content = res.content.decode()

# 1. 先获取父容器
container = re.search(r'pic-auto pic-list">(.*?)<div class="layout auto mt15">', content, re.S)
if container:
	# 2. 在父容器内部获取列表
	items = re.findall(r'<a href="(.*?)".*?data-original="(.*?)".*?</a>', container.group(1), re.S)
	for item in items:
		href = f"https://www.hdbz.net{item[0]}"
		print(f"正在获取详情页", href)
		detail_res = requests.get(href)
		detail_info = re.search(r'<div .*? class="pic-preview">.*?<img alt="(.*?)" src="(.*?)" />.*?</div>', detail_res.content.decode(), re.S)
		if detail_info:
			detail_image_name = detail_info.group(1)
			detail_image_url = detail_info.group(2)

			detail_img_res = requests.get(detail_image_url)
			with open(f"./images/{detail_image_name}_detail.jpg", "wb") as f:
				f.write(detail_img_res.content)

			thumb_url = item[1]
			thumb_img_res = requests.get(thumb_url)
			with open(f"./images/{detail_image_name}_thumb.jpg", "wb") as f:
				f.write(thumb_img_res.content)
		else:
			print(f"可能遇见了合集")
		time.sleep(1)
		# break

